package com.wbl.train.business.controller.admin;
import cn.hutool.core.util.ObjectUtil;
import com.wbl.train.business.service.TrainCarriageService;
import com.wbl.train.common.business.dto.TrainCarriageDto;
import com.wbl.train.common.business.entity.TrainCarriageEntity;
import com.wbl.train.common.exception.ApplicationException;
import com.wbl.train.common.page.PageVo;
import com.wbl.train.common.result.R;
import com.wbl.train.common.result.ResultCode;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 王博纶
 * @since 2024-02-02
 * @description 火车车厢
 */
@AllArgsConstructor
@Slf4j
@RestController
@RequestMapping("/admin/train-carriage")
@Tag(name = "火车车厢控制器类",
        description = "TrainCarriageController 火车车厢 后端数据接口")
public class TrainCarriageAdminController {
    private final TrainCarriageService trainCarriageService;

    /**
     * 通过id查询 火车车厢
     * @param trainCarriageDto 火车车厢Dto
     * @return Result
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/query")
    public R<PageVo<TrainCarriageEntity>> getById(TrainCarriageDto trainCarriageDto) {
        // 火车车次查询
        log.debug("trainCarriageDto :{}",trainCarriageDto);
        PageVo<TrainCarriageEntity> pageVo = trainCarriageService.queryTrainCarriage(trainCarriageDto);
        return R.ok("查询成功",pageVo);
    }

    /**
     * 新增 火车车厢
     * @param trainCarriage 火车车厢
     * @return Result
     */
    @Operation(summary = "新增火车车厢", description = "新增火车车厢")
    @PostMapping("/save")
    public R<Boolean> save(@Valid @RequestBody TrainCarriageEntity trainCarriage) {
        return R.ok("保存成功",trainCarriageService.insertAndUpdateTrain(trainCarriage));
    }

    /**
     * 通过id删除 火车车厢
     * @param id id
     * @return Result
     */
    @Operation(summary = "通过id删除火车车厢",
               description = "通过id删除火车车厢")
    @DeleteMapping("/delete/{id}")
    public R<Boolean> removeById(@PathVariable Long id) {
        if(ObjectUtil.isNull(trainCarriageService.getById(id))){
            throw new ApplicationException(ResultCode.DATA_NOT_EXIST,"当前火车车厢信息不存在");
        }
        return R.ok(trainCarriageService.removeById(id));
    }

}
